﻿using _1._13._2015_Art.Data;
using _1._13._2015_Art.Data.Models;
using _1._13._2015_Art.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace _1._13._2015_Art.Controllers
{
    public class ArtPieceController : ApiController
    {
        public IHttpActionResult Get()
        {
            List<ArtPieceViewModel> models = null;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {

                models = db.ArtPieces.Select(a => new ArtPieceViewModel
                {
                    ArtistId = a.ArtistID,
                    ArtUrl = a.ArtUrl,
                    Description = a.Description,
                    ArtPieceId = a.ArtPieceId
                }).ToList();
            }
            return Ok(models);
        }

        // add new art piece
        [Authorize]
        public IHttpActionResult Post(ArtPieceViewModel model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                ArtPiece piece = new ArtPiece();
                piece.ArtistID = model.ArtistId;
                piece.ArtUrl = model.ArtUrl;
                piece.Description = model.Description;
                               
                db.ArtPieces.Add(piece);
                db.SaveChanges();                              
            }
            return Ok();
        }


    }
}
